package Android.Billarm;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;

public class Billarm extends BroadcastReceiver
{
    public static void resetAlarm (Context context)
    {
        DB_SQLite db = new DB_SQLite (context);
        db.open ();
        Cursor c = db.fetchActiveBillarms ();
        c.moveToPosition (-1);

        if (c.moveToNext ())
        {
            setAlarm (context, DB_SQLite.cursorToBill (c));
        }
        else
        {
            NotificationManager nm = (NotificationManager)
                context.getSystemService (Context.NOTIFICATION_SERVICE);
            nm.cancelAll ();
            setAlarm (context, null);
        }

        c.close ();
        db.close ();
    }


    public static void setAlarm (Context context, Bill bill)
    {
        Log.d ("Billarm", "sending broadcast");

        AlarmManager mgr = (AlarmManager)
            context.getSystemService (Context.ALARM_SERVICE);

        Intent intent = new Intent (context, Billarm.class);
        intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);

        if (bill != null)  intent.putExtra ("bill", bill.id);

        PendingIntent pending = PendingIntent.getBroadcast
            (context, 1, intent, PendingIntent.FLAG_ONE_SHOT);

        if (bill != null)
        {
            mgr.set (AlarmManager.RTC,
                    bill.getBillarmDate().getTimeInMillis(),
                    pending);
            Log.d ("Billarm", "done sending broadcast");
        }
        else
        {
            mgr.cancel (pending);
            Log.d ("Billarm", "done cancelling broadcast");
        }
    }

    // Display an alert that we've received a message.    
    @Override 
    public void onReceive (Context context, Intent intent)
    {
        Log.d ("Billarm", "receiving broadcast");

        Bill bill;
        {
            DB_SQLite db = new DB_SQLite (context);
            db.open ();
            long id = intent.getLongExtra ("bill", -1);
            if (id < 0)
            {
                Log.d ("Billarm", "Received bill without valid id!");
                db.close ();
                return;
            }
            bill = db.fetchBill (id);
            db.close ();
        }

        // Send a text notification to the screen.
        NotificationManager nm = (NotificationManager)
            context.getSystemService (Context.NOTIFICATION_SERVICE);

        /* Instantiate notification */
        int icon = R.drawable.logo;
        CharSequence tickerText = "Bill-arm goes off!";
        long when = System.currentTimeMillis ();

        Notification notification = new Notification (icon, tickerText, when);

        /* Define the Notification's expanded message and Intent: */
        CharSequence contentTitle = "Bill Reminder";
        CharSequence contentText = "A bill must soon be paid!";

        Intent notifIntent = new Intent (context, BillScreen.class);

        bill.dirtyp = true; /* User may have changed it! */
        notifIntent.putExtra ("bill", bill);

        PendingIntent contentIntent =
            PendingIntent.getActivity (context, 0, notifIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

        notification.setLatestEventInfo (context, contentTitle, contentText, contentIntent);

        /* Pass the Notification to the NotificationManager: */
        nm.notify (1, notification);
        Log.d ("Billarm", "done receiving broadcast");
    }
}

